package org.csu.myPetStore.web.servlet;

import com.sun.org.apache.xpath.internal.operations.Or;
import org.csu.myPetStore.domain.Account;
import org.csu.myPetStore.domain.Order;
import org.csu.myPetStore.service.LogcatService;
import org.csu.myPetStore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Date;
import java.util.List;

public class ViewOrderListServlet extends HttpServlet {

    private static final String VIEW_ORDER_LIST = "/WEB-INF/jsp/order/ListOrders.jsp";

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        HttpSession session = req.getSession();
        Account account = (Account) session.getAttribute("account");
        LogcatService logcatService = new LogcatService();

        OrderService service = new OrderService();

        List<Order> orderList = service.getOrdersByUsername(account.getUsername());

        session.setAttribute("orderList",orderList);

        Order order = (Order) session.getAttribute("order");
        if(order == null){
            order = new Order();
            session.setAttribute("order",order);
        }


        java.util.Date date = new java.util.Date();
        Date datesql = new Date(date.getTime());
        logcatService.InsertLogCat(account.getUsername(),account.getUsername() + " View OrderList List at " + datesql.toString());

        req.getRequestDispatcher(VIEW_ORDER_LIST).forward(req,resp);
    }

}
